package com.dtlaya.allutils.dcmThreadUploadInsertDatebase.service;

import com.dtlaya.allutils.dcmThreadUploadInsertDatebase.entity.DicomEntities;
import com.dtlaya.allutils.dcmFileUpload.service.DicomStorageService;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

@Service
public class DicomBatchService {
//
//    private final Executor taskExecutor; // 自定义线程池
//    private final DicomStorageService storageService;
//    private final DicomDatabaseService databaseService;
//
//    // 批处理大小
//    private static final int BATCH_SIZE = 50;
//
//    public DicomBatchService(@Qualifier("dicomTaskExecutor") Executor taskExecutor,
//                             DicomStorageService storageService,
//                             DicomDatabaseService databaseService) {
//        this.taskExecutor = taskExecutor;
//        this.storageService = storageService;
//        this.databaseService = databaseService;
//    }
//
//    @Async
//    public CompletableFuture<Void> processBatch(List<MultipartFile> files) {
//        // 分批处理
//        for (int i = 0; i < files.size(); i += BATCH_SIZE) {
//            int end = Math.min(i + BATCH_SIZE, files.size());
//            List<MultipartFile> batch = files.subList(i, end);
//
//            // 并行处理每个文件
//            List<CompletableFuture<DicomEntities>> futures = batch.stream()
//                .map(file -> CompletableFuture.supplyAsync(
//                    () -> processSingleFile(file),
//                    taskExecutor
//                ))
//                .toList();
//
//            // 等待所有任务完成
//            List<DicomEntities> entities = futures.stream()
//                .map(CompletableFuture::join)
//                .toList();
//
//            // 批量保存到数据库
//            databaseService.batchSave(entities);
//        }
//        return CompletableFuture.completedFuture(null);
//    }
//
//    private DicomEntities processSingleFile(MultipartFile file) {
//        try {
//            // 1. 解析DICOM元数据
//            //Attributes attributes = storageService.parseDicomAttributes(file);
//
//            // 2. 生成存储路径并保存文件
//            //Path filePath = storageService.storeDicomFile(file, attributes);
//
//            // 3. 构建实体对象
//            return buildEntities(attributes, filePath);
//        } catch (Exception e) {
//            throw new DicomProcessingException("处理DICOM文件失败: " + file.getOriginalFilename(), e);
//        }
//    }
//
//    private DicomEntities buildEntities(Attributes attributes, Path filePath) {
////        DicomEntities entities = new DicomEntities();
////
////        // 构建Patient实体
////        Patient patient = new Patient();
////        patient.setPatientId(attributes.getString(Tag.PatientID));
////        patient.setPatientName(attributes.getString(Tag.PatientName));
////        patient.setPatientBirthDate(attributes.getDate(Tag.PatientBirthDate));
////        patient.setPatientSex(attributes.getString(Tag.PatientSex));
////        entities.setPatient(patient);
////
////        // 构建Study实体
////        Study study = new Study();
////        study.setStudyInstanceUid(attributes.getString(Tag.StudyInstanceUID));
////        study.setStudyDate(attributes.getDate(Tag.StudyDate));
////        study.setStudyTime(attributes.getDate(Tag.StudyTime));
////        study.setReferringPhysician(attributes.getString(Tag.ReferringPhysicianName));
////        entities.setStudy(study);
////
////        // 构建Series实体
////        Series series = new Series();
////        series.setSeriesInstanceUid(attributes.getString(Tag.SeriesInstanceUID));
////        series.setSeriesNumber(attributes.getInt(Tag.SeriesNumber, 0));
////        series.setModality(attributes.getString(Tag.Modality));
////        series.setBodyPartExamined(attributes.getString(Tag.BodyPartExamined));
////        entities.setSeries(series);
////
////        // 构建Image实体
////        Image image = new Image();
////        image.setSopInstanceUid(attributes.getString(Tag.SOPInstanceUID));
////        image.setFilePath(filePath.toString());
////        image.setFileSize(Files.size(filePath));
////        entities.setImage(image);
//
//        return entities;
//    }
}